package me.springboot.mybatis.zhcs.utils;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huawei.iotplatform.client.NorthApiClient;
import com.huawei.iotplatform.client.dto.*;
import com.huawei.iotplatform.client.invokeapi.Authentication;
import com.huawei.iotplatform.client.invokeapi.DeviceManagement;
import com.huawei.iotplatform.client.invokeapi.SignalDelivery;
import com.huawei.iotplatform.utils.JsonUtil;

import java.util.Date;

public class NorthwardUtils {
    public static void createClient(String deviceId,String HexStr)throws Exception{
        NorthApiClient nac = new NorthApiClient();
        ClientInfo ci = new ClientInfo();

        String appId = "EcV3n3xLtHIMPtK_MItfVqZzzmAa";
        String secret = "Z0pHYJx8yCEpEO9fuAuhoyw3tsca";

        ci.setAppId(appId);
        ci.setPlatformIp("180.101.147.89");
        ci.setPlatformPort("8743");
        ci.setSecret(secret);

        nac.setClientInfo(ci);
        nac.initSSLConfig();
        DeviceManagement dm = new DeviceManagement(nac);
        //获取用户
        Authentication aaa = new Authentication(nac);
        //获取token
        AuthOutDTO aod = aaa.getAuthToken();
        String accessToken = aod.getAccessToken();

        SignalDelivery sd = new SignalDelivery();
        sd.setNorthApiClient(nac);
        PostDeviceCommandInDTO pdcid = new PostDeviceCommandInDTO();
        pdcid.setDeviceId(deviceId);
        AsynCommandDTO acdo = new AsynCommandDTO();
        String serviceId = "NbIotDevice";
        String method = "SET_DEVICE_LEVEL";
        ObjectNode paras = JsonUtil.convertObject2ObjectNode("{\"MsgInforDown\":\""+HexStr+"\"}");
        acdo.setServiceId(serviceId);
        acdo.setMethod(method);
        acdo.setParas(paras);
        pdcid.setCommand(acdo);
        PostDeviceCommandOutDTO pdcod = sd.postDeviceCommand(pdcid, appId, accessToken);
        System.out.println(pdcod.toString());
    }

    public static void main(String[] a)throws Exception{
        String deviceId = "1c6705b6-c0a5-40c9-a6e1-2b1499fac232";
        byte[] bytes = new byte[9];
        String dateStr = DateUtils.getLongToDateTime(new Date().getTime());
        System.out.println(dateStr);
        String[] dd = dateStr.split(" ");
        String[] date = dd[0].split("-");
        String[] time = dd[1].split(":");
        bytes[0] = 0x55;        //接收数据状态0x55 成功
        //设置时间
        bytes[1] = ByteUtils.intToByteArray(Integer.parseInt(date[0]) - 2000)[0];
        bytes[2] = ByteUtils.intToByteArray(Integer.parseInt(date[1]))[1];
        bytes[3] = ByteUtils.intToByteArray(Integer.parseInt(date[2]))[1];

        bytes[4] = ByteUtils.intToByteArray(Integer.parseInt(time[0]))[1];
        bytes[5] = ByteUtils.intToByteArray(Integer.parseInt(time[1]))[1];
        bytes[6] = ByteUtils.intToByteArray(Integer.parseInt(time[2]))[1];

        bytes[7] = (byte)0xAA;      //是否设置参数 0xAA 否*/
        bytes[8] = 0x00;
        NorthwardUtils.createClient(deviceId,ByteUtils.BinaryToHexString(bytes));
    }
}
